package com.woniu.qqmaster.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.qqmaster.entity.Orders;
import com.woniu.qqmaster.entity.User;
import com.woniu.qqmaster.entity.UserBo;
import org.apache.ibatis.annotations.*;
import org.springframework.cache.annotation.Cacheable;

import java.util.List;

/**
 * @Author dlb
 * @Date 2022/12/27 16:30
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("select * from user where id = #{id}")
    @Results({
            @Result(column = "id",property = "orders",many = @Many(select = "getOrdersByUserId"))
    })
    User getUserById(@Param("id") Long id);

    @Select("select * from orders where uid = #{uid}")
    List<Orders> getOrdersByUserId(@Param("uid") Long uid);

    // ------------------------------------------------------------------
    // 联表查询：查询一个订单同时，顺带把他对应的人员查出来
    @Select("select * from orders where oid = #{oid}")
    @Results({
            @Result(column = "uid" , property = "user" , one = @One(
                    // 后面这个结果只能一行
                    select = "getUserById"
            ))
    })
    Orders findOrderById(Long oid);

    // 根据用户id来查询用户


    @Select("select u.id , u.`name` , u.age , u.email,\n" +
            "\t\to.oid , o.money , o.state , o.receiveInfo , o.phoNum , o.order_time , o.uid , accepter\tfrom `user` u inner join orders o on (u.id = o.uid) \n" +
            "\t\t\n" +
            "\t\twhere o.oid = #{oid}")
    @Results({
            @Result(column = "id",property = "user.id"),
            @Result(column = "money",property = "orders.money")
    })
    // 它很智能，它可以把方法的返回值 UserBo 保存到缓存里面去...
    @Cacheable("UserBoInfo")
    UserBo findUserBoByOid(@Param("oid") Long oid);


}
